define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var muiTips = require("muiTips"); //封装的弹框
	var globalInit = require("globalInit"); //共用js
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');

	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityPurchaseDet = authorityInit.getJurisdiction(authorityData, sourceType.BUY); //采购记录权限判断
	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}
	if(authorityPurchaseDet.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作采购单的权限，请联系管理员开通吧！', 2, 3, "default");
		$("header > a").remove();
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$("header > a").show();
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}
	if(authorityPurchaseDet.indexOf(2) === -1) {
		$('#creatSellOut').remove();
	} else {
		$('#creatSellOut').show();
	}
	var eHistory = require("eHistory"); //调用审批历史数据
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	/*a链接跳转问题*/
	mui('#editForMore').on('tap', 'a', function() {
		document.location.href = this.href + "?dd_nav_bgcolor=FFFF943E";
	});
	//接收审批中心的数据
	var SourceId = sessionStorage.getItem('SourceId');
	var AuditId = sessionStorage.getItem('AuditId');
	var Audit_detailId = sessionStorage.getItem('Audit_detailId');
	var SourceType = sessionStorage.getItem('SourceType');
	//接收首页采购订单列表的数据
	var PirchaseID = sessionStorage.getItem('PirchaseID');
	if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')) {
		var outPSourceType = sessionStorage.getItem('outPSourceType');
		var outPSourceID = sessionStorage.getItem('outPSourceID');
	}

	function historyAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
	}

	function historyNoAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
	}
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		switch($(this).text()) {
			case '重置草稿':
				muiTips.confirmTips(' ', '您确定要重置吗！', 2, 1, resetDraft);
				//				var btnResetArray = ['否', '是'];
				//				mui.confirm('<ul class="mui-row pop-tabmain">' +
				//					'<li class="mui-col-xs-4 mui-col-sm-4"style="text-align: center;">' +
				//					'<a class="yuanico"><span style="margin-top: 6px;">!</span></a></li>' +
				//					'<li class="mui-col-xs-8 mui-col-sm-8" style="text-align: left;margin-top: 16px;">' +
				//					'您确定要重置吗！</li></ul>', ' ', btnResetArray,
				//					function(e) {
				//
				//						if(e.index == 1) {
				//							resetDraft();
				//
				//						} else {
				//							//否
				//						}
				//					}, 'div');
				break;
			case '通过':
				muiTips.alertTips(' ', '恭喜您已通过！', 1, 1, historyAudit);
				//				mui.alert('<ul class="mui-row pop-tabmain">' +
				//					'<li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;">' +
				//					'<a class="yuanico"><span class="icoTransform">√</span></a></li>' +
				//					'<li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 20px;margin-top: 16px;">' +
				//					'恭喜您已通过！</li></ul>', ' ',
				//					function() {
				//						eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
				//					}, 'div');
				break;
			case '驳回':
				muiTips.alertTips(' ', '驳回成功！', 1, 1, historyNoAudit);
				//				mui.alert('<ul class="mui-row pop-tabmain">' +
				//					'<li class="mui-col-xs-6 mui-col-sm-6"style="text-align: right;padding-right: 20px;">' +
				//					'<a class="yuanico"><span class="icoTransform">√</span></a></li>' +
				//					'<li class="mui-col-xs-6 mui-col-sm-6" style="text-align: left;line-height: 20px;margin-top: 16px;">' +
				//					'驳回成功！</li></ul>', ' ',
				//					function() {
				//						eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
				//					}, 'div');
				break;
			case '编辑':
				break;
			case '删除':
				muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, deleteBuy);
				//				var btnArray = ['否', '是'];
				//				mui.confirm('<ul class="mui-row pop-tabmain">' +
				//					'<li class="mui-col-xs-4 mui-col-sm-4"style="text-align: center;">' +
				//					'<a class="yuanico"><span style="margin-top: 6px;">!</span></a></li>' +
				//					'<li class="mui-col-xs-8 mui-col-sm-8" style="text-align: left;margin-top: 16px;">' +
				//					'您确定要删除吗！</li></ul>', ' ', btnArray,
				//					function(e) {
				//
				//						if(e.index == 1) {
				//							deleteBuy();
				//						} else {
				//							//否
				//						}
				//					}, 'div');
				break;
		}
	});
	var option = {};
	var dataModel = {};
	if(sessionStorage.getItem('SourceId') && sessionStorage.getItem('AuditId') && sessionStorage.getItem('Audit_detailId') && sessionStorage.getItem('SourceType')) {
		//审批中心入口
		dataModel = {
			"id": sessionStorage.getItem('SourceId')
		};
		//初始化页面显示
		$('header > a').attr('href', '#eForMore');

		//调用审批历史数据
		setTimeout(function() {
			eHistory.historyCon('sHistoryList', '#historyList', SourceType, SourceId);
		}, 1000);
		//钉钉返回清缓存
		dingTalkNav.LeftNavBar(clearSessionSPDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionSPDD);
		//返回清除审批中心订单缓存
		mui('body').on('tap', 'header > button', function() {
			clearSessionSP();
		});
	} else {
		//首页入口
		dataModel = {
			"id": PirchaseID
		};

		//生成采购入库单
		mui('body').on('tap', '#creatSellOut', function() {
			sessionStorage.removeItem('outPSourceType');
			sessionStorage.removeItem('outPSourceID');

			//			    sessionStorage.removeItem('purchaseStastusId');
			sessionStorage.setItem('serialNo', $('#serialNo').text());
			sessionStorage.setItem('purDetID', $('#serialNo').attr("data-id"));
			document.location.href = '../newBuy/newBuyInOrder.html?dd_nav_bgcolor=FFFF943E';
		});
		//查看已生成采购入库单
		mui('body').on('tap', '#querySellOut', function() {
			sessionStorage.removeItem('outPSourceType');
			sessionStorage.removeItem('outPSourceID');
			sessionStorage.removeItem('PirchaseID');
			sessionStorage.removeItem('sellData');
			sessionStorage.setItem('inPurchaseID', $('#serialNo').attr("data-id"));
			document.location.href = '../outPurchase/index.html?dd_nav_bgcolor=FFFF943E';
		});
		//返回清除首页订单缓存
		//  	if(sessionStorage.getItem('outPSourceType') && sessionStorage.getItem('outPSourceID')){
		mui('body').on('tap', 'header > button', function() {
			clearSession();
		});
		// 		}
	}
	//钉钉清除缓存
	dingTalkNav.LeftNavBar(clearSessionDD);
	//ad
	dingTalkNav.leftAndroid(clearSessionDD);

	//返回清除缓存 钉钉
	function clearSessionDD() {
		sessionStorage.removeItem('outPSourceType');
		sessionStorage.removeItem('outPSourceID');
		sessionStorage.removeItem('sellData');
		sessionStorage.removeItem('purchaseStastusId');
		sessionStorage.removeItem('PirchaseID');
		sessionStorage.removeItem('goodsIndex');
		document.location.href = "../../src/purchase/index.html?dd_nav_bgcolor=FFFF943E";
	}
	//返回清除缓存→审批 钉钉
	function clearSessionSPDD() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
		document.location.href = "../Examination/index.html?dd_nav_bgcolor=FFFF943E";
	}

	//返回清除缓存
	function clearSession() {
		sessionStorage.removeItem('outPSourceType');
		sessionStorage.removeItem('outPSourceID');
		sessionStorage.removeItem('sellData');
		sessionStorage.removeItem('purchaseStastusId');
		sessionStorage.removeItem('PirchaseID');
		sessionStorage.removeItem('goodsIndex');
	}
	//返回清除缓存→审批
	function clearSessionSP() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
	}

	abp.services.app.buyList.getModel(dataModel, option).
	done(function(data) {
		var isRef = data.isRef;
		//接收首页传过来的状态值
		var purchaseStastusId = data.statusId;

		if(isRef === true) {
			$("#querySellOut").show();
			$("#sellFixeddiv").show();
		} else {
			$("#querySellOut").hide();
		}
		////待入库/待出库
		if(purchaseStastusId == abp.statusId.Pending || purchaseStastusId == abp.statusId.Processing || purchaseStastusId == abp.statusId.Finish) {
			$('header > a').attr('href', '#forMore');
		} else {
			$('header > a').attr('href', '#editForMore');
		}
		if(purchaseStastusId == abp.statusId.Pending || purchaseStastusId == abp.statusId.Processing) {
			$("#creatSellOut").show();
			$("#sellFixeddiv").show();
		} else {
			$("#creatSellOut").hide();
			//			$("#sellFixeddiv").hide();
		}
		//		////待入库/待出库
		//		if(purchaseStastusId == abp.statusId.Pending) {
		//			$("#creatSellOut").show();
		//			$("#sellFixeddiv").show();
		//		}
		//		//入库中/出库中/盘点中
		//		if(purchaseStastusId == abp.statusId.Processing) {
		//			$("#creatSellOut").show();
		//			$("#querySellOut").show();
		//			$("#sellFixeddiv").show();
		//		}
		//		//完成、审核
		//		if(purchaseStastusId == abp.statusId.Finish) {
		//			$("#creatSellOut").hide();
		//			$("#querySellOut").show();
		//			$("#sellFixeddiv").show();
		//		}

		var purchaseData = JSON.stringify(data);
		sessionStorage.setItem('sellData', purchaseData); //生成采购入库单时使用
		//单据详情绑定
		$('#statusName').text(data.statusName);
		$('#statusName').attr("data-statusId", data.statusId);
		$('#serialNo').text(data.serialNo);
		$('#serialNo').attr("data-id", data.id);
		$('#serialNo').attr("data-statusId", data.statusId);
		$('#serialDate').text(data.serialDate.substring(0, 10));
		$('#deliveryDate').text(data.deliveryDate.substring(0, 10));

		$('#companyName').text(data.companyName);
		$('#companyName').attr("data-id", data.companyId);
		$('#busUserName').text(data.busUserName);
		$('#busUserName').attr("data-id", data.busUserId);
		var texttaxRateType = '';
		switch(data.taxRateType) {
			case 1:
				texttaxRateType = '不计税';
				break;
			case 2:
				texttaxRateType = '不含税价';
				break;
			case 3:
				texttaxRateType = '含税价';
				break;
		}
		$('#taxRateType').text(texttaxRateType);
		$('#taxRateType').attr("data-id", data.taxRateType);
		$('#totalAmount').text(data.totalAmount);
		$('#operatorName').text(data.operatorName);
		$('#dataCreated').text(data.dataCreated.substring(0, 10));
		$('#notes').text(data.notes);

		//状态颜色
		var statusIdColor = require("colorOrder");
		statusIdColor("#statusName");
		//货品数据明细绑定
		var data1 = {};
		data1.list = data.buyDetail;
		var html = template('pirchaseDetList', data1);
		$("#listPurchaseUl").html(html);

		//审批历史数据绑定
		if(data.success) {

		} else {
			//	            		console.log(data.error.message)

		}
	}).fail(function(data) {
		var muiTips = require("muiTips");
		muiTips.alertTips(' ', data.message, 2, 3, "default");
	});

	//重置草稿
	function resetDraft() {
		var option = {};
		var dataModel = {
			"id": sessionStorage.getItem('PirchaseID'),
			"serialNo": $('#serialNo').text()
		};
		abp.services.app.buyList.resetDraft(dataModel, option).
		done(function(data) {
			document.location.href = "../../src/purchase/index.html?dd_nav_bgcolor=FFFF943E";
			if(data.success) {

			} else {
				//	            		console.log(data.error.message)		
			}
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});

	}

	//删除单据
	function deleteBuy() {
		var option = {};
		var dataModel = {
			"id": sessionStorage.getItem('PirchaseID'),
			"serialNo": $('#serialNo').text()
		};
		abp.services.app.buyList.delete(dataModel, option).
		done(function(data) {
			document.location.href = "../../src/purchase/index.html?dd_nav_bgcolor=FFFF943E";
			if(data.success) {

			} else {
				//	            		console.log(data.error.message)

			}
		}).fail(function(data) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', data.message, 2, 3, "default");
		});
	}

	globalInit.eHistoryInit("#historyList"); //审批历史数据提示

});